//
//  PreView.swift
//  picture查看器（上下手势）
//
//  Created by cvicseks-mac1 on 16/3/14.
//  Copyright © 2016年 cvicseks-mac1. All rights reserved.
//

import UIKit

enum PreViewDirection // 手势方向
{
    case Up, Down
}
class PreView: UIImageView {

    // direction
    var direction:PreViewDirection!
    // 对象数组
    var imageViewArray:Array<UIImageView>!
    // 图片数组
    var imageArray:Array<UIImage>!
    // 当前下表
    var currentIndex:Int!
    var nextIndex:Int!
    
    init(frame: CGRect,imageNameArray:Array<String>) {
        super.init(frame: frame)
        self.currentIndex = 0;
        self.nextIndex = 1;
        self.setImageViewArray()
        self.setImageNameArray(imageNameArray)
        self.initGesture()
    }
    
    func setImageViewArray(){
        if(self.imageViewArray==nil){
            // 初始化
            self.imageViewArray = Array<UIImageView>()
            // 初始化两个imageview作为载体
            for(var i = 0; i < 2; i++){
                let imageView = UIImageView(frame: self.bounds)
                if(i==0){
                    imageView.hidden = false;
                }
                else{
                    imageView.hidden = true;
                }
                // 添加到父视图
                self.addSubview(imageView)
                self.imageViewArray.append(imageView)
            }
        }
    }
    
    func setImageNameArray(imageNameArray:Array<String>){
        if(self.imageArray == nil){
            self.imageArray = Array<UIImage>()
        }
        // 给视图添加默认的图片
        for(var i = 0 ; i < imageNameArray.count-1 ; i++){
            let image = UIImage(named: imageNameArray[i])
            self.imageArray.append(image!)
        }
        let currentImageView = self.imageViewArray[0];
        let nextImageView = self.imageViewArray[1];
        currentImageView.image = self.imageArray[0];
        nextImageView.image = self.imageArray[1];
    }
    
    func setCurrentIndex(Index:Int){
        self.currentIndex = Index
        if(self.currentIndex < 0){
            self.currentIndex = self.imageArray.count - 1;
        }
        if(self.currentIndex > self.imageArray.count - 1){
            self.currentIndex = 0;
        }
    }
    
    func setNextIndex(Index:Int){
        self.nextIndex = Index
        if(self.nextIndex < 0){
            self.nextIndex = self.imageArray.count - 1;
        }
        if(self.nextIndex > self.imageArray.count - 1){
            self.nextIndex = 0;
        }
    }
    // 添加手势
    func initGesture(){
        // 防止
        self.userInteractionEnabled = true
        let pan = UIPanGestureRecognizer(target: self, action: "panhandle")
        self.addGestureRecognizer(pan)
    }
    
    func panhandle(pan:UIPanGestureRecognizer){
        // 判断方向
        // 滑动的速度，有正负值，向下为正
        let velocity = pan.velocityInView(pan.view)
        if(pan.state == UIGestureRecognizerState.Began){
            if(velocity.y > 0)
            {
                self.direction = PreViewDirection.Down
                self.setNextIndex(self.currentIndex - 1)
            }else{
                self.direction = PreViewDirection.Up
                self.setNextIndex(self.currentIndex + 1)
            }
        }
        self.handleUpDownPan(pan)
    }
    //初始化imageview位置
    // 什么时候初始化：手指按下的时候
    func handleUpDownPan(pan:UIPanGestureRecognizer){
        // 重置显示的图片
        let currentImageView = self.imageViewArray[0];
        let nextImageView = self.imageViewArray[1];
        currentImageView.image = self.imageArray[self.currentIndex];
        nextImageView.image = self.imageArray[self.nextIndex];
        //
        if(pan.state == UIGestureRecognizerState.Began){
            // 初始化位置
            // 要显示哪个imageview 下一张
            nextImageView.hidden = false
            var begin = nextImageView.frame
            let height = self.bounds.size.height
            if(self.direction == PreViewDirection.Down){
                begin.origin.y = height
            }else{
                begin.origin.x = 0
            }
            // 改变netview的位置
            nextImageView.frame = begin
            // 将nextimageview置顶（覆盖当前页）
            self.bringSubviewToFront(nextImageView)
        }
        self.setTranslationAnimate(pan, nextImageView: nextImageView)

    }
    
    // 动画
    func setTranslationAnimate(pan:UIPanGestureRecognizer,nextImageView:UIImageView){
        // 相对于上一个点的坐标
        let translationY = pan.translationInView(self).y
        var nextFrame = nextImageView.frame
        nextFrame.origin.y += translationY
        nextImageView.frame = nextFrame
        pan.setTranslation(CGPoint.zero, inView: pan.view)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
